import sys
import json

'''
读取全局配置文件
'.\sys_config.json'
'''
sys_config = {}


def PrintErrorAndExit(val):
    print('无法识别的配置文件----' + val)
    sys.exit(-1)


# 全局配置
if len(sys.argv) != 2:
    print('未指定配置文件路径,系统即将退出. . .')
    sys.exit(-1)

configPath = sys.argv[1]
configPath = configPath.replace('\\', '/')
with open(configPath, encoding='utf-8') as f:
    config = json.load(f)

    if 'startType' not in config:
        PrintErrorAndExit('startType')

    sys_config['startType'] = config['startType']
    # 数据库、文件夹两种模式
    if config['startType'] == 'file':
        if 'servicesListPath' not in config:
            PrintErrorAndExit('servicesListPath')

        sys_config['servicesListPath'] = config['servicesListPath']
    elif config['startType'] == 'db':
        if 'DBConnectInfo' not in config:
            PrintErrorAndExit('DBConnectInfo')
        if 'host' not in config:
            PrintErrorAndExit("host")
        else:
            sys_config['host'] = config['host']

        if 'port' not in config:
            PrintErrorAndExit("port")
        else:
            sys_config['port'] = config['port']
        sys_config['DBConnectInfo'] = config['DBConnectInfo']
    else:
        PrintErrorAndExit('startType')

    if 'debug' not in config:
        sys_config['debug'] = True
    else:
        sys_config['debug'] = bool(config['debug'])

    if 'host' not in config:
        PrintErrorAndExit("host")
    else:
        sys_config['host'] = config['host']

    if 'port' not in config:
        PrintErrorAndExit("port")
    else:
        sys_config['port'] = config['port']

